package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.DoctorAdvice;
import com.his.service.DoctorAdviceService;
import com.his.utils.MyUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * (DoctorAdvice)表控制层
 *
 * @author makejava
 * @since 2020-10-23 18:07:47
 */
@Validated
@RestController
@RequestMapping("doctorAdvice")
public class DoctorAdviceController {
    /**
     * 服务对象
     */
    @Resource
    private DoctorAdviceService doctorAdviceService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public DoctorAdvice selectOne(Integer id) {
        return this.doctorAdviceService.queryById(id);
    }

    @RequireRoles(roles = {"doctor","admin"}, index = 0)
    @PostMapping("getAll")
    public Rsp getAll(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<DoctorAdvice> pageParam) throws Exception {
        return doctorAdviceService.queryAll(pageParam,MyUtil.getUser(token));
    }

    @RequireRoles(roles = {"doctor"}, index = 0)
    @PostMapping("add")
    public Rsp add(@RequestHeader(Const.TOKEN) String token,@Validated @RequestBody DoctorAdvice advice) throws Exception {
        advice.setDoctorId(MyUtil.getUserByToken(token));
        return doctorAdviceService.insert(advice);
    }

    @RequireRoles(roles = {"doctor","nurse"}, index = 0)
    @PostMapping("update")
    public Rsp update(@RequestHeader(Const.TOKEN) String token, @RequestBody DoctorAdvice advice) {
        return doctorAdviceService.update(advice);
    }


    @RequireRoles(roles = {"doctor"}, index = 0)
    @DeleteMapping("stop/{id}")
    public Rsp stop(@RequestHeader(Const.TOKEN) String token, @NotNull(message = "处方id不能为空") @PathVariable("id") Integer id) {
        DoctorAdvice advice = new DoctorAdvice();
        advice.setId(id);
        advice.setStatus("2");
        advice.setTreatmentEndtime(new Date());
        return doctorAdviceService.update(advice);
    }

    /**
     * 通过病人id得到所有医嘱
     *
     * @param token
     * @param patientId
     * @return
     */
    @RequireRoles(roles = {"doctor", "nurse"}, index = 0)
    @GetMapping("selectAllByPatientId")
    public Rsp selectAllByPatientId(@RequestHeader(Const.TOKEN) String token, @NotBlank String patientId) {
        return Rsp.success(doctorAdviceService.selectAllByPatientId(patientId));
    }


    /**
     * 更新医嘱
     *
     * @param token
     * @param doctorAdvice
     * @return
     */
    @RequireRoles(roles = {"doctor", "nurse"}, index = 0)
    @PostMapping("updateOne")
    public Rsp updateOne(@RequestHeader(Const.TOKEN) String token, @RequestBody DoctorAdvice doctorAdvice) {
        return Rsp.success(doctorAdviceService.update(doctorAdvice));
    }
}
